package com.qiangesoft.weather.gaode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.weather.gaode.entity.WeatherForecast;
import com.qiangesoft.weather.gaode.mapper.WeatherForecastMapper;
import com.qiangesoft.weather.gaode.service.IWeatherForecastService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 预报天气 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @since 2023-08-10
 */
@Service
public class WeatherForecastServiceImpl extends ServiceImpl<WeatherForecastMapper, WeatherForecast> implements IWeatherForecastService {

    @Override
    public List<WeatherForecast> forecastWeather(String adcode) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.now();
        String today = localDate.format(formatter);
        String format1 = localDate.plusDays(1).format(formatter);
        String format2 = localDate.plusDays(2).format(formatter);

        LambdaQueryWrapper<WeatherForecast> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeatherForecast::getAdcode, adcode)
                .in(WeatherForecast::getDate, Arrays.asList(today, format1, format2))
                .orderByDesc(WeatherForecast::getId)
                .last("limit 3");

        List<WeatherForecast> weatherForecastList = baseMapper.selectList(queryWrapper);
        weatherForecastList.sort(Comparator.comparing(WeatherForecast::getId));
        return weatherForecastList;
    }
}
